﻿<?php
 /*
 * @Author Akos Szabo - Axi
 * Created on Jul 5, 2011
 * @Project: ujej
 * @Description: This class is for handling user
 * 
 * @TODO:
 * -class skeleton
 * -isAdmin
 * -getMenu
 * -getSport 
 */
 
 //Global variables
 $oUser;
 
 //alapadatok legyenek memcache-ban
class user_handler
{
	//ezt a user_group táblából kell venni. 
 var $uid = NULL;
 var $GROUP_ADMIN = 1;//memcache
 var $GROUP_REGISTERED = 3;//memcache
 var $dbh;
 var $args = array();
 //actions
 var $ACTION_REGISTER = "REGISTER";
 var $ACTION_LOGOUT = "LOGOUT";
 var $ACTION_LOGIN = "LOGIN";
 var $ACTION_FORGET_PW = "FORGET_PW";
 
 
 /**
  * This is the constructor.
  * Loads in basic user data
  */
 function user_handler( $argDbh, $argUid="", $argMail="" )
 {
 	
 	$this->dbh = $argDbh;
 	
 	if( $argUid<>"")
 	{
 		$this->loadDataById($argUid);
 	}
 	else 
 	{
 		if($argMail<>"")
 		{
 			$this->loadDataByMail($argMail);
 		}
 	}
 }
 
 
 /**
  * check if the user exist based on the user id.
  */
 function doesUserExist()
 {
 	
 	if($this->uid <> NULL)
 	return true;
 	 
 	return false;
 }
 
 /**
  * Having the ID get user data
  */
 function loadDataById( $argId )
 {
 	$query = "SELECT * from users WHERE iduser=".$argId;
 	$this->loadDataByQuery($query);
 }
 
 
 /**
  * Having the mail get user data.
  */
 function loadDataByMail( $argMail )
 {
 	$query = "SELECT * FROM users WHERE mail='".$argMail."'";
 	$this->loadDataByQuery($query);
 }
 
 /**
  * input is the query is built based on the id or mail. Load user data by query
  */
 function loadDataByQuery( $argQuery )
 {
 	$resNum = $this->dbh->do_query( $argQuery );
 	if( $resNum > 0 )
 	{
 		$res = $this->dbh->get_data();
 		$this->loadData($res);
 	}
 }
 
 /**
  * having the result of the query this loads data into the object
  */
 function loadData( $argRes )
 {
 	$this->args["mail"] = $argRes["mail"];
 	$this->args["id"] = $argRes["iduser"];
 	$this->args["userGroup"] = $argRes["iduser_group"];
 	$this->args["nick"] = $argRes["nick"];
 	$this->args["phone"] = $argRes["phone"];
 	$this->args["idSport"] = $argRes["idsport"];
 	$this->args["sportName"] = $argRes["sport_name"];
 	$this->args["regdate"] = $argRes["regdate"];
 	$this->uid = $argRes["iduser"];
 }
 
/**
 * returns arguments get by its name
 */
function getArgument($argArg)
{
	return $this->args[$argArg];
}


/**
 * set argument of the user object
 */
function setArgument($argName, $argValue)
{
	//!!! should write back to the DB
	$this->args[$argName] = $argValue;
}

/**
 * returns the user id
 */
function getId()
{
	return $this->uid;
}

 /**
  * registers a new user based on the arguments
  */
 function registerUser( $argMail, $argPw, $argSportName )
 {
 	$pw = md5($argPw);
 	$regDate = date("Y-m-d H:i:s");
 	$query = "INSERT INTO users (iduser_group, mail, pw, sport_name, regdate) "
 	." VALUES(".$this->GROUP_REGISTERED.", '".$argMail."', '".$pw."', '".$argSportName."', '".$regDate."' )";
// 	INSERT INTO `users` (`iduser`, `iduser_group`, `nick`, `mail`, `phone`, `pw`, `idsport`, `sport_name`, `regdate`) VALUES
//(1, 1, 'axi', 'axi@ujej.hu', '', 'axi', 0, '', '2011-04-01 20:30:26')
 	$resNum = $this->dbh->do_query( $query );
 	$this->uid = $this->dbh->getLastAutoId();
 	
 	$this->setArgument("id", $this->uid);
 	$this->setArgument("mail", $argMail);
 	$this->setArgument("sportName", $argSportName);
 	
 	$this->writeActionLog($this->ACTION_REGISTER); 
 	
 	return $this->uid;	
 }
 
 /**
  * write log into the DB
  */
 function writeActionLog( $argAction )
 {
 	$query = "INSERT INTO user_log (iduser, action, time_stamp) VALUES(".$this->uid.", '".$argAction."', ".time().")";
 	$resNum = $this->dbh->do_query( $query );
 }
 
 /**
  * Login user based on mail and pw
  */
 function loginUser( $argMail, $argPw )
 {
 	$query = "SELECT * FROM users WHERE mail='".$argMail."'";
 	$resNum = $this->dbh->do_query( $query );
 	if( $resNum > 0 )//does such mail exist?
 	{
 		$res = $this->dbh->get_data();
 		$inPw = md5( $argPw );
 		$stPw =$res["pw"];
 		if( $inPw == $stPw )//is the pw the same?
 		{
 			$this->loadData( $res );
 			$this->writeActionLog( $this->ACTION_LOGIN);
 			return $this->getArgument("id");		
 		}
 	} 
 	
 	return -1; 	
 }
 
 
 /**
  * logout
  */
 function logout()
 {
 	$this->writeActionLog( $this->ACTION_LOGOUT);
 	
 }
 
 
 /**
  * 
  */
 function getMenu()
 { 	
 }
 
 
 
}//end of class
?>
